define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	const controller = {
		index() {
			Table.api.init({
				extend: {
					index_url: 'business/highsea/index',
					recovery_url: 'business/highsea/recovery',
					apply_url: 'business/highsea/apply',
					del_url: 'business/highsea/del',
					table: 'business',
				},
			});

			const table = $('#table');

			table.bootstrapTable({
				// 请求地址
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				// 设置工具栏元素
				toolbar: '.toolbar',
				// 设置主键
				pk: 'id',
				// 排序字段
				sortName: 'id',
				// 默认排序方式
				sortOrder: 'desc',
				showExport: false,
				// 渲染表格数据
				columns: [
					{ checkbox: true },
					{ field: 'id', title: 'ID', operate: false },
					{ field: 'nickname', title: __('Bnickname'), operate: false },
					{ field: 'source.name', title: __('Bsource') },
					{ field: 'hide_mobile_text', title: __('Bmobile'), operate: false },
					{ field: 'gender_text', title: __('Gender') },
					{
						field: 'deal',
						title: __('BdealText'),
						searchList: { 0: __('未成交'), 1: __('已成交') },
						formatter: Table.api.formatter.normal,
					},
					{
						field: 'auth',
						title: __('BAuthStatus'),
						searchList: { 0: __('未认证'), 1: __('已认证') },
						formatter: Table.api.formatter.normal,
					},
					{
						field: 'operate',
						title: __('Operate'),
						table: table,
						events: Table.api.events.operate,
						formatter: Table.api.formatter.operate,
						buttons: [
							{
								name: 'recovery',
								title: '分配',
								classname: 'btn btn-xs btn-success btn-dialog',
								extend: "data-toggle='tooltip'",
								icon: 'fa fa-exchange',
								url: $.fn.bootstrapTable.defaults.extend.recovery_url,
							},
							{
								name: 'apply',
								title: '领取',
								classname: 'btn btn-xs btn-info btn-ajax',
								extend: "data-toggle='tooltip'",
								icon: 'fa fa-address-book-o',
								confirm: '确认领取？',
								url: $.fn.bootstrapTable.defaults.extend.apply_url,
								success(data, ret) {
									Layer.alert(ret.msg);
									// 刷新表格
									table.bootstrapTable('refresh');
								},
								error(data, ret) {
									console.log(data, ret);
									Layer.alert(ret.msg);
									return false;
								},
							},
						],
					},
				],
      });
      
      // 分配
      $('.btn-recovery').click(function () {
        const ids = Table.api.selectedids(table).toString();
        Fast.api.open($.fn.bootstrapTable.defaults.extend.recovery_url + "?ids=" + ids, '分配')
      })

      // 领取
      $('.btn-apply').click(function () {
        const ids = Table.api.selectedids(table).toString();
        layer.confirm('确定领取？', {
          title: '提示',
          btn: ['确定', '取消'],
        }, index => { 
          // 发送请求
          $.post($.fn.bootstrapTable.defaults.extend.apply_url, { ids: ids }, function (res) {
            if (res.code == 1) {
              Toastr.success(res.msg);
              // 刷新表格
              table.bootstrapTable('refresh');
            } else {
              Toastr.error(res.msg);
            }
          }, 'json')

          layer.close(index);
        })
      })

			Table.api.bindevent(table);
		},
		recovery() {
			controller.api.bindevent();
		},
		apply() {
			controller.api.bindevent();
		},
		del() {
			controller.api.bindevent();
		},
		api: {
			// JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
